//
//  WBStatusPictrureView.swift
//  仿微博
//
//  Created by apple on 2016/12/28.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class WBStatusPictrureView: UIView {
    
    var viewModel: WBStatusViewModel? {
        didSet{
            calcViewSize()
        }
    
    }
    
    /// 根据视图模型的配图视图大小，调整显示内容
    private func calcViewSize(){
        //处理宽度
        //1> 单图，根据配图视图的大小，修改 subview[0] 的高度
        if viewModel?.picURLs?.count == 1 {
            let  viewSize = viewModel?.pictureViewSize ?? CGSize()
         //获取第0个 图像视图
            let v = subviews[0]
            v.frame = CGRect(x: 0, y: WBStatusPictureViewOutterMargin, width: viewSize.width, height: viewSize.height - WBStatusPictureViewOutterMargin)
        }else{
        //2> 多图， 回复 subview[0]的宽高，保证九宫格布局的完整
            let v = subviews[0]
            v.frame = CGRect(x: 0, y: WBStatusPictureViewOutterMargin, width: WBStatusPictureViewItemWidth, height: WBStatusPictureViewItemWidth)
        
        }
        
        
        
        
        //修改高度约束
         heightCons.constant = viewModel?.pictureViewSize.height ?? 0
    }
    /// 配图视图的数组
    var urls: [WBStatusPicture]? {
        didSet {
        //1.隐藏所有的  imageView
            for v in subviews {
                v.isHidden = true
            }
        //2.遍历 urls 数组 ，顺序设置图像
            var index = 0
            for url in urls ?? [] {
                //获得对应索引的  imageView
                let iv = subviews[index] as! UIImageView
                // 4 张 图像 处理
                if index == 1 && urls?.count == 4 {
                index += 1
                
                }
                
                
                //设置图像
                iv.cz_setImage(urlString: url.thumbnail_pic, placeholderImage: nil)
                //
                iv.isHidden = false
                index += 1
            }
        
        }
    
    }
    
    
    
    @IBOutlet weak var heightCons: NSLayoutConstraint!
    
    
    
    override func awakeFromNib() {
      setupUI()
        
    }
    

    
    
    
    
    
    
    
}

// MARK: - 设置界面
extension WBStatusPictrureView {
    
    //  Cell 中所有的控件都是提前准备好的
    // 设置的时候 ，根据数据决定是否显示
    //不要动态创建控件
    fileprivate func setupUI() {
        
        //设置背景颜色
        backgroundColor = superview?.backgroundColor
        /// 超出边界的不显示
        clipsToBounds = true
        let count = 3
        let rect = CGRect(x: 0,
                          y: WBStatusPictureViewOutterMargin,
                          width: WBStatusPictureViewItemWidth,
                          height: WBStatusPictureViewItemWidth)
    //循环 创建 9  个 imageView
        for i in 0..<count * count{
            let iv = UIImageView()
            //设置 contentMode
            iv.contentMode = .scaleAspectFill
            iv.clipsToBounds = true
            
            //行
            let row = CGFloat(i / count)
            //列
            let col = CGFloat(i % count)
            let xoffset = col * (WBStatusPictureViewItemWidth + WBStatusPictureViewOInnerMargin)
            let yoffset = row * (WBStatusPictureViewItemWidth + WBStatusPictureViewOInnerMargin)
            iv.frame = rect.offsetBy(dx: xoffset, dy: yoffset)
            addSubview(iv)
        }
    
    }




}
